package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Annotation.AdminAccess;
import com.weixin.project.Entity.PracticeHistory;
import com.weixin.project.Entity.Question;
import com.weixin.project.Entity.QuestionBank;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.*;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class PracticeController {
    @Value("${logging.pattern.dateformat}")
    String dateFormat;
    @Autowired
    UserService userService;
    @Autowired
    PracticeService practiceService;
    @Autowired
    QuestionService questionService;
    @Autowired
    RecommendService recommendService;
    @PostMapping("/text/submit_my_answer")
    JSONObject submitAnswer(HttpServletRequest request,
                            @RequestBody JSONObject params){
        User user = userService.findCurrentUser(request);
        Question question = questionService.findQuestionById(params.getInteger("id"));
        PracticeHistory practiceHistory = new PracticeHistory();
        practiceHistory.setUserId(user.getId());
        practiceHistory.setCreateTime(new Date());
        JSONArray answers = params.getJSONArray("my_answer");
        practiceHistory.setAnswer(answers.toString());
        practiceHistory.setQuestionId(question.getId());
        practiceHistory.setTypeId(question.getTypeId());
        JSONObject ret = new JSONObject();
        practiceService.practice(practiceHistory);
        ret.put("is_right", JSONArray.parseArray(practiceHistory.getFlags()));
        return ResponseUtil.success("", ret);
    }

    @GetMapping("/test/get_my_answer")
    JSONObject getMyAnswers(HttpServletRequest request,
                            @RequestParam("id") Integer questionId){
        JSONObject ret = new JSONObject();
        User user = userService.findCurrentUser(request);
        if (user == null) {
            return ResponseUtil.userNotFind();
        }
        List<PracticeHistory> histories = practiceService.findUserPracticesOfOneQuestion(questionId, user.getId());
        JSONArray jsonArray = new JSONArray();
        for(PracticeHistory history:histories){
            jsonArray.add(JSONArray.parseArray(history.getAnswer()));
        }
        ret.put("my_answer", jsonArray);
        return ResponseUtil.success("", ret);
    }

    /**
     * 获取当前用户上次做题的位置
     * @param request request
     * @param chapterId 章节id
     * @return
     */
    @GetMapping("/chapter/get_my_page")
    JSONObject getMyLoc(HttpServletRequest request,
                        @RequestParam("chapter_id") Integer chapterId){
        JSONObject ret = new JSONObject();
        User currentUser = userService.findCurrentUser(request);
        if (currentUser == null) {
            return ResponseUtil.userNotFind();
        }
        ret.put("page", practiceService.findLastPracticeLoc(currentUser.getId(), chapterId)+1);
        return ResponseUtil.success("",ret);
    }

    @GetMapping("/test/all_history")
    JSONObject getAllHistories(HttpServletRequest request,
                               @RequestParam("page") Integer page){
        JSONObject ret = new JSONObject();
        User currentUser = userService.findCurrentUser(request);
        page -- ;
        List<PracticeHistory> practiceHistories = practiceService.findUserPracticeHistories(currentUser.getId(), page);
        ret.put("total_pages", practiceService.countUserPracticeHistoryTotalPage(currentUser.getId()));
        JSONArray arr = new JSONArray();
        for (PracticeHistory practiceHistory :
                practiceHistories) {
            Question question = questionService.findQuestionById(practiceHistory.getQuestionId());
            JSONObject obj = new JSONObject();
            obj.put("question_id", practiceHistory.getQuestionId());
            obj.put("question_content", question.getStem());
            obj.put("create_time", new SimpleDateFormat(dateFormat).format(practiceHistory.getCreateTime()));
            obj.put("chapter", questionService.findChapter(question.getChapterId()).getChapterName());
            obj.put("chapter_id", question.getChapterId());
            obj.put("bank_id", question.getQuestionBankId());
            QuestionBank questionBank = questionService.findQuestionBankById(question.getQuestionBankId());
            obj.put("bank", questionBank == null?"":questionBank.getBankName());
            arr.add(obj);
        }
        ret.put("practice_histories", arr);
        return ResponseUtil.success("", ret);
    }

}
